$.extend($.fn,{
    createPage:function(options){
        this.total = options.total||0;
        this.currentPage =options.currentPage||1;
        this.pageNum=options.pageNum||5;
        this.onPageLoading=options.onPageLoading;
        if(options.width){
            this.width=options.width+"px";
        }else{
            this.width='auto';
        }
        var content = this;
        this.addClass("s_page");
        content.empty();
        this.prevBtn=$('<button class="s_btn" style="width:'+this.width+'">&lt;</button>').appendTo(content);
        this.pageBtnContent = $("<span></span>").appendTo(content);
        this.nextBtn=$('<button class="s_btn" style="width:'+this.width+'">&gt;</button>').appendTo(content);
        this.toTextField=$('<input type="text" class="s_textfield" style="width:50px;"/>').appendTo(content);
        this.jumpBtn=$('<button class="s_btn">跳转</button>').appendTo(content);
        this.refresh();
        var self=this;
        this.prevBtn.click(function(){
            self.refresh(self.currentPage-1);
        })
        this.nextBtn.click(function(){
            self.refresh(self.currentPage+1);
        })
        this.jumpBtn.click(function(){
            self.refresh(parseInt(self.toTextField.val()||1));
        })
        $(this.pageBtnContent).on('click','button',function(){
            self.refresh(parseInt($(this).text()));
        })
    },
    refresh:function(page){
        var pageSize = parseInt((this.total-1)/this.pageNum)+1;
        page=page<1?1:page;
        page=page>pageSize?pageSize:page;
        this.currentPage=page||this.currentPage;
        var startPageIndex=this.currentPage-parseInt(this.pageNum/2);
        startPageIndex=startPageIndex<1?1:startPageIndex;
        var temp = pageSize-this.pageNum+1;
        if(temp>0) startPageIndex=startPageIndex>temp?temp:startPageIndex;
        var endPageIndex=startPageIndex+this.pageNum-1;
        endPageIndex=endPageIndex>pageSize?pageSize:endPageIndex;
        this.pageBtnContent.empty();
        for(var i=startPageIndex;i<=endPageIndex;i++){
            $('<button class="s_btn page_btn'+(i===this.currentPage?' s_btn-select':'')+'" style="width:'+this.width+'">'+i+'</button>').appendTo(this.pageBtnContent);
        }
        if(typeof(this.onPageLoading)==='function'){
            this.onPageLoading(page);
        }
    }
})